import tempfile
import base64


class Save(object):
    """
    图片临时保存, 跟随类实例销毁
    """
    def __init__(self):
        self.temp_dir = tempfile.TemporaryDirectory()
        self.path = self.temp_dir.name

    def FullPath(self, name):
        return f'{self.path}\\{name}'

    def base64To(self, name, content):
        img_bytes = base64.b64decode(content)
        return self.bytesTo(img_bytes, name)

    def bytesTo(self, name, content):
        full_path = self.FullPath(name)
        with open(full_path, 'wb') as f:
            f.write(content)
        return full_path
